/*     */ package com.zimbra.cs.service.admin;
/*     */ 
/*     */ import com.google.common.base.Splitter;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AdminConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.accesscontrol.AdminRight;
/*     */ import com.zimbra.cs.account.accesscontrol.Rights.Admin;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxIndex;
/*     */ import com.zimbra.cs.mailbox.MailboxIndex.ReIndexStatus;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.soap.JaxbUtil;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.admin.message.ReIndexRequest;
/*     */ import com.zimbra.soap.admin.type.ReindexMailboxInfo;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class ReIndex
/*     */   extends AdminDocumentHandler
/*     */ {
/*     */   private static final String ACTION_START = "start";
/*     */   private static final String ACTION_STATUS = "status";
/*     */   private static final String ACTION_CANCEL = "cancel";
/*     */   private static final String STATUS_STARTED = "started";
/*     */   private static final String STATUS_RUNNING = "running";
/*     */   private static final String STATUS_IDLE = "idle";
/*     */   private static final String STATUS_CANCELLED = "cancelled";
/*  60 */   private static final String[] TARGET_ACCOUNT_PATH = { "mbox", "id" };
/*     */   
/*     */ 
/*     */ 
/*     */   protected String[] getProxiedAccountPath()
/*     */   {
/*  66 */     return TARGET_ACCOUNT_PATH;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public boolean domainAuthSufficient(Map<String, Object> context)
/*     */   {
/*  74 */     return true;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public boolean defendsAgainstDelegateAdminAccountHarvesting()
/*     */   {
/*  82 */     return true;
/*     */   }
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  88 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  89 */     ReIndexRequest req = (ReIndexRequest)JaxbUtil.elementToJaxb(request);
/*     */     
/*  91 */     String action = req.getAction();
/*     */     
/*  93 */     ReindexMailboxInfo reIndexMboxInfo = req.getMbox();
/*  94 */     String accountId = reIndexMboxInfo.getAccountId();
/*     */     
/*  96 */     Provisioning prov = Provisioning.getInstance();
/*  97 */     Account account = prov.get(Key.AccountBy.id, accountId, zsc.getAuthToken());
/*  98 */     defendAgainstAccountOrCalendarResourceHarvesting(account, Key.AccountBy.id, accountId, zsc, Rights.Admin.R_reindexMailbox, Rights.Admin.R_reindexCalendarResourceMailbox);
/*     */     
/* 100 */     Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account, false);
/* 101 */     if (mbox == null) {
/* 102 */       throw ServiceException.FAILURE("mailbox not found for account " + accountId, null);
/*     */     }
/*     */     
/* 105 */     Element response = zsc.createElement(AdminConstants.REINDEX_RESPONSE);
/*     */     
/* 107 */     if ("start".equalsIgnoreCase(action)) {
/* 108 */       if (mbox.index.isReIndexInProgress()) {
/* 109 */         response.addAttribute("status", "running");
/*     */       } else {
/* 111 */         String typesStr = reIndexMboxInfo.getTypes();
/* 112 */         String idsStr = reIndexMboxInfo.getIds();
/*     */         
/* 114 */         if ((typesStr != null) && (idsStr != null)) {
/* 115 */           ServiceException.INVALID_REQUEST("Can't specify both 'types' and 'ids'", null);
/*     */         }
/*     */         
/* 118 */         if (typesStr != null) {
/*     */           Set<MailItem.Type> types;
/*     */           try {
/* 121 */             types = MailItem.Type.setOf(typesStr);
/*     */           } catch (IllegalArgumentException e) {
/* 123 */             throw MailServiceException.INVALID_TYPE(e.getMessage());
/*     */           }
/* 125 */           mbox.index.startReIndexByType(types);
/* 126 */         } else if (idsStr != null) {
/* 127 */           Set<Integer> ids = new HashSet();
/* 128 */           for (String id : Splitter.on(',').trimResults().split(idsStr)) {
/*     */             try {
/* 130 */               ids.add(Integer.valueOf(Integer.parseInt(id)));
/*     */             } catch (NumberFormatException e) {
/* 132 */               ServiceException.INVALID_REQUEST("invalid item ID: " + id, e);
/*     */             }
/*     */           }
/* 135 */           mbox.index.startReIndexById(ids);
/*     */         } else {
/* 137 */           mbox.index.startReIndex();
/*     */         }
/*     */         
/* 140 */         response.addAttribute("status", "started");
/*     */       }
/* 142 */     } else if ("status".equalsIgnoreCase(action)) {
/* 143 */       MailboxIndex.ReIndexStatus status = mbox.index.getReIndexStatus();
/* 144 */       if (status != null) {
/* 145 */         addProgressInfo(response, status);
/* 146 */         response.addAttribute("status", "running");
/*     */       } else {
/* 148 */         response.addAttribute("status", "idle");
/*     */       }
/* 150 */     } else if ("cancel".equalsIgnoreCase(action)) {
/* 151 */       MailboxIndex.ReIndexStatus status = mbox.index.cancelReIndex();
/* 152 */       if (status != null) {
/* 153 */         response.addAttribute("status", "cancelled");
/* 154 */         addProgressInfo(response, status);
/*     */       } else {
/* 156 */         response.addAttribute("status", "idle");
/*     */       }
/*     */     } else {
/* 159 */       throw ServiceException.INVALID_REQUEST("Unknown action: " + action, null);
/*     */     }
/*     */     
/* 162 */     return response;
/*     */   }
/*     */   
/*     */   private void addProgressInfo(Element response, MailboxIndex.ReIndexStatus status) {
/* 166 */     Element prog = response.addElement("progress");
/* 167 */     prog.addAttribute("numSucceeded", status.getProcessed() - status.getFailed());
/* 168 */     prog.addAttribute("numFailed", status.getFailed());
/* 169 */     prog.addAttribute("numRemaining", status.getTotal() - status.getProcessed());
/*     */   }
/*     */   
/*     */   public void docRights(List<AdminRight> relatedRights, List<String> notes)
/*     */   {
/* 174 */     relatedRights.add(Rights.Admin.R_reindexMailbox);
/* 175 */     relatedRights.add(Rights.Admin.R_reindexCalendarResourceMailbox);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/admin/ReIndex.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */